Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catching fetch errors #4931

Merged
merged 3 commits into from
Oct 30, 2024
Merged

catching fetch errors #4931

merged 3 commits into from
Oct 30, 2024

Conversation

jonathanlurie
Copy link
Contributor

Originally opened as #4822 but closed because of some git branch issue.

Launch Checklist

  • Confirm your changes do not include backports from Mapbox projects (unless with a compliant license) - if you are not sure about this, please ask!
  • Briefly describe the changes in this PR.
  • Link to related issues.
  • Include before/after visuals or gifs if this PR includes visual changes.
  • Write tests for all new functionality. Read bellow
  • Document any changes to public APIs.
  • Post benchmark scores.
  • Add an entry to CHANGELOG.md under the ## main section.

Description

This PR addresses how failed HTTP requests due to CORS policy are handled. So far, if a request fails because of CORS, it does not return a response, instead, it makes the browser throw a generic TypeError ( see here ). As a result, a map instance subscribing to the "error" event would catch a generic error that does not include the faulty URL. It is better to handle a CORS error with an AJAXError, that contains information about the URL and the cause of the actual problem.

This is especially useful in the context of loading a style.json so that if a given style is not reachable because of CORS, it is possible to catch this error with the same efficiency as if fetching a style.json would yield an HTTP 5xx status code, since in both cases the request would fails due to server configuration.

This fix also addresses other network issues such as DNS resolution failures or malformed URLs

Before / After

With this minimalist snippet:

const map = new maplibregl.Map({
  container: 'map',
  style: "https://example.com/style.json",
});

map.on("error", (err) => {
  console.log(">>> ", err);
}) 

Here is the console output,

  • BEFORE:
Screenshot 2024-10-11 at 09 59 01
  • AFTER:
Screenshot 2024-10-11 at 10 03 41

Test

I tried adding another test to src/util/ajax.test.ts but it is impossible to simulate a CORS with the Nise package's FakeServer, since a CORS error is not part of the server response but is due to the browser blocking it.

Other info

  • This makes no change to public API.
  • this does not affect performance

@codecov-commenter
Copy link

codecov-commenter commented Oct 30, 2024

Codecov Report

Attention: Patch coverage is 0% with 6 lines in your changes missing coverage. Please review.

Project coverage is 90.43%. Comparing base (9d9a600) to head (d8e12be).
Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
src/util/ajax.ts 0.00% 6 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4931   +/-   ##
=======================================
  Coverage   90.42%   90.43%           
=======================================
  Files         265      265           
  Lines       38098    38103    +5     
  Branches     3203     3189   -14     
=======================================
+ Hits        34452    34459    +7     
- Misses       2665     2683   +18     
+ Partials      981      961   -20     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@HarelM HarelM enabled auto-merge (squash) October 30, 2024 10:33
@HarelM HarelM merged commit d67fc04 into maplibre:main Oct 30, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants